/**
 * Copyright 2006-2015 handu.com
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.iepacj.sjdbc.demo1;

import com.alibaba.druid.pool.DruidDataSource;
import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class Demo1 {

    private static final String JDBC_URL = "jdbc:mysql://%s:3306/%s?useUnicode=true&characterEncoding=UTF-8" +
            "&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true";

    private static final String USERNAME = "okdilife";

    private static final String PASSWORD = "okdilife";

    public static void main(String[] args) throws SQLException {
        System.out.println(String.format(JDBC_URL, "192.168.31.204", "okdilife"));

        /**
         *  数据源配置
         */
        Map<String, DataSource> dataSourceMap = new HashMap<>();
        dataSourceMap.put("ds_0", createDataSource("localhost", "ds_0"));
        dataSourceMap.put("ds_1", createDataSource("localhost", "ds_1"));
        DataSourceRule dsRule = new DataSourceRule(dataSourceMap);

        /**
         * 分片规则配置
         */



        //ShardingRule shardingRule = new ShardingRule(dsRule, tableRules, bindingTableRules, dsShardingStrgy, tableShardingStrgy);

    }

    /**
     * 创建数据源
     * @param dsHost 数据源ip地址
     * @param dsName 数据库名
     * @return       数据源
     * @throws SQLException
     */
    public static DataSource createDataSource(String dsHost, String dsName) throws SQLException {
        DruidDataSource ds = new DruidDataSource();
        ds.setUrl(String.format(JDBC_URL, dsHost, dsName));
        ds.setUsername(USERNAME);
        ds.setPassword(PASSWORD);
        // 初始化连接池大小
        ds.setInitialSize(100);
        // 连接池最大连接数量
        ds.setMaxActive(300);
        ds.setMinIdle(10);
        ds.setMaxWait(3000);
        ds.setTimeBetweenEvictionRunsMillis(60000);
        ds.setFilters("log4j");
        return ds;
    }


}
